# -*- coding = utf-8 -*-

import time
import arrow
import datetime

from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium import webdriver
from assertpy import assert_that, soft_assertions
import simplejson as json


class Basic (object):
    def __init__(self, test_environment):
        self.env_type = test_environment
        # 全局变量 driver
        self.driver = webdriver.Chrome()
        # 当前毫秒
        self.current_mis_time = int(round(time.time() * 1000))
        # 明天
        self.tomorrow = str(datetime.date.today() + datetime.timedelta(days=1))
        # 当前秒
        self.current_second = arrow.now().timestamp
        # 当前日期
        self.current_date = str(datetime.date.today())
        # 域名
        self.service_data = {"app_test4": {"host": "https://pt4webf.xbongbong.com.cn", "login_url": "/stand-alone-login.html#/", "element_file_path": "E:/Xbb/WebUI/elements/app_test4.json"},
                             "app_production": {"host": "https://appwebfront.xbongbong.com", "login_url": "/stand-alone-login.html#/", "element_file_path": "/Users/mc/WebUI/elements/app_production.json"},
                             "ding_test1": {"host": "https://ptwebf.xbongbong.com.cn", "element_file_path": "/Users/mc/WebUI/elements/ding_test1.json", "login_url": "/auto-login.html?t=eqraQNCqDIBpOkkj+/JM1MRtVLJwtGbMq9zuyE4hiasfW7v7zsD7NSDomnAcrjYQ8pAX8ouxp6I=&nonce=cnlz5h&env=dd&corpid=ding18b527cbc48f835535c2f4657eb6378f"},
                             "ding_production": {"host": "https://pfweb.xbongbong.com", "element_file_path": "/Users/mc/WebUI/elements/ding_production.json", "login_url": "/auto-login.html?t=eqraQNCqDIBpOkkj+/JM1MRtVLJwtGbMq9zuyE4hiasfW7v7zsD7NSDomnAcrjYQ8pAX8ouxp6I=&nonce=e6hxyf&env=dd&corpid=ding18b527cbc48f835535c2f4657eb6378f"}}

    # 解析元素信息
    def parser_element(self, element_key):
        element_file = self.service_data[self.env_type]["element_file_path"]
        with open(element_file, mode='r+', encoding='utf-8') as data:
            page_elements = json.load(data)
            return page_elements[element_key]

    # 定位元素
    def find_element(self, element_key):
        try:
            element_info = self.parser_element(element_key)
            method = element_info['method']
            locator = element_info['path']
            # 根据定位元素的方法区分
            if 'xpath' == method:
                WebDriverWait(self.driver, 30).until(lambda the_driver:the_driver.find_element(by=By.XPATH, value=locator).is_enabled())
                element = self.driver.find_element(by=By.XPATH, value=locator)
                ActionChains(self.driver).move_to_element(element).perform()
                return self.driver.find_element(by=By.XPATH, value=locator)
            elif 'class' == method:
                WebDriverWait(self.driver, 30).until(lambda the_driver:the_driver.find_element(by=By.CLASS_NAME, value=locator).is_enabled())
                element = self.driver.find_element(by=By.CLASS_NAME, value=locator)
                ActionChains(self.driver).move_to_element(element).perform()
                return self.driver.find_element(by=By.CLASS_NAME, value=locator)
            elif 'id' == method:
                WebDriverWait(self.driver, 30).until(lambda the_driver:the_driver.find_element(by=By.ID, value=locator).is_enabled())
                element = self.driver.find_element(by=By.ID, value=locator)
                ActionChains(self.driver).move_to_element(element).perform()
                return self.driver.find_element(by=By.ID, value=locator)
            elif 'tag' == method:
                WebDriverWait(self.driver, 30).until(lambda the_driver:the_driver.find_element(by=By.TAG_NAME, value=locator).is_enabled())
                element = self.driver.find_element(by=By.TAG_NAME, value=locator)
                ActionChains(self.driver).move_to_element(element).perform()
                return self.driver.find_element(by=By.TAG_NAME, value=locator)
            elif 'link' == method:
                WebDriverWait(self.driver, 30).until(lambda the_driver:the_driver.find_element(by=By.LINK_TEXT, value=locator).is_enabled())
                element = self.driver.find_element(by=By.LINK_TEXT, value=locator)
                ActionChains(self.driver).move_to_element(element).perform()
                return self.driver.find_element(by=By.LINK_TEXT, value=locator)
            else:
                pass
        except Exception:
            self.driver.get_screenshot_as_png()

    # 查找一组元素
    def find_elements(self, element_key):
        try:
            element_info = self.parser_element(element_key)
            method = element_info['method']
            locator = element_info['path']
            if 'class' == method:
                time.sleep(1)
                elements = self.driver.find_elements(by=By.CLASS_NAME, value=locator)
                return elements
            elif 'tag' == method:
                time.sleep(1)
                return self.driver.find_elements(by=By.TAG_NAME, value=locator)
            elif 'xpath' == method:
                time.sleep(1)
                return self.driver.find_elements(by=By.XPATH, value=locator)
            else:
                pass
        except Exception:
            self.driver.get_screenshot_as_png()

    # 登录系统
    def login(self):
        host = self.service_data[self.env_type]["host"]
        login_url = self.service_data[self.env_type]["login_url"]
        # 打开登录页
        self.driver.get(host + login_url)
        # 最大化浏览器窗口
        self.driver.maximize_window()
        # 判断平台(钉钉vs独立版)s
        if 'app' in str(self.env_type):
            # 输入用户名和密码
            self.input('user_name', '15838495721')
            self.input('user_password', 'Yxy123456')
            self.click('login_agreement')
            if self.find_element('user_verification_code') is None:
                self.click('login_button1')
            else:
                self.input('user_verification_code', '8888')
                self.click('login_button2')
            # 选择某公司
            self.company_select('回归测试公司')
            self.driver.find_element(by=By.XPATH, value='//*[@class="el-form input-block"]/div[2]/div/button').click()
        else:
            pass
        time.sleep(4)

    # 退出系统
    def logout(self):
        # 点击用户后退出系统
        self.click('person_name')
        self.click('sign_out')

    # 独立版选择公司进行登录
    def company_select(self, company_name):
        company_list = self.parser_element('company_list')['path']
        WebDriverWait(self.driver, 20).until(lambda the_driver: the_driver.find_element(by=By.CLASS_NAME, value=company_list).is_displayed())
        item_list = self.driver.find_element(by=By.CLASS_NAME, value=company_list).find_elements(by=By.CLASS_NAME, value='company-name')
        for item in item_list:
            if str(item.text).__contains__(company_name):
                item.click()
                break
            else:
                continue

    # 刷新当前页面
    def refresh_page(self):
        self.driver.refresh()
        time.sleep(4)

    # 跳转到指定列表，支持测试4和生产环境
    def go_to(self, page_name):
        service_host = self.service_data[self.env_type]["host"]
        page_url = self.parser_element(page_name)
        self.driver.get(service_host + page_url + str(self.current_mis_time))
        time.sleep(4)
        self.refresh_page()

    # 跳转到指定列表，支持测试4和生产环境
    def to_print_page(self, page_name):
        service_host = self.service_data[self.env_type]["host"]
        page_url = self.parser_element(page_name)
        self.driver.get(service_host + page_url)
        time.sleep(4)
        self.refresh_page()

    # 通用 列表页右上角的操作
    def operation_on_list(self, operation_name):
        # 详情页触发新建入口的下拉选项
        operation_items_on_list = self.parser_element('operation_on_list')['path']
        # 触发列表页的操作
        WebDriverWait(self.driver, 30).until(lambda the_driver:the_driver.find_element(by=By.CLASS_NAME, value=operation_items_on_list).is_enabled())
        item_list = self.driver.find_element(by=By.CLASS_NAME, value=operation_items_on_list).find_elements(by=By.TAG_NAME, value='span')
        for item in item_list:
            if str(item.text).__contains__(operation_name):
                item.click()
                break
            else:
                continue
        time.sleep(4)

    # 点击元素
    def click(self, element_key):
        element_data = self.find_element(element_key)
        ActionChains(self.driver).move_to_element(element_data).click().perform()
        time.sleep(4)

    # 输入内容
    def input(self, element_key, input_value):
        element = self.find_element(element_key)
        time.sleep(1)
        try:
            element.clear()
            element.send_keys(input_value)
        except Exception as msg:
            raise msg

    # 获得元素文本
    def get_element_text(self, element_key):
        element = self.find_element(element_key)
        return element.text

    # 从列表进入数据详情
    def go_to_data_detail(self, data_name):
        WebDriverWait(self.driver, 30).until(lambda the_driver:the_driver.find_element(by=By.LINK_TEXT, value=data_name).is_enabled())
        data_link_on_list = self.driver.find_element(by=By.LINK_TEXT, value=data_name)
        ActionChains(self.driver).move_to_element(data_link_on_list).click().perform()
        time.sleep(4)

    # (通用)上下结构页面列表页的全选
    def select_all_on_list(self):
        self.click("select_all_on_list")

    # 列表批量编辑 action_index是按钮的下标
    def batch_operation_on_list(self, action_index):
        locator = self.parser_element("batch_operate_on_list")['path']
        WebDriverWait(self.driver, 30).until(
            lambda the_driver: the_driver.find_element(by=By.CLASS_NAME, value=locator).is_enabled())
        batch_action_list = self.driver.find_element(by=By.CLASS_NAME, value=locator).find_elements(by=By.CLASS_NAME, value='item')
        batch_action_button = batch_action_list[action_index].find_element(by=By.TAG_NAME, value="button")
        ActionChains(self.driver).move_to_element(batch_action_button).click().perform()
        time.sleep(2)

    # 批量编辑-选择要修改的字段  fieldName为字段名称
    def clickAndChoose_batchEdit_field(self, fieldName):
        self.click('choose_batchEdit_field')
        time.sleep(1)
        locator = self.parser_element('batchEdit_field_list')['path']
        WebDriverWait(self.driver, 30).until(lambda the_driver:the_driver.find_element(by=By.XPATH, value=locator).is_enabled())
        item_list = self.driver.find_element(by=By.XPATH, value=locator).find_elements(by=By.TAG_NAME, value='span')
        for item in item_list:
            if str(item.text).__contains__(fieldName):
                item.click()
                break
            else:
                continue

    # 批量编辑-选择字段值 fieldValue为具体值
    def clickAndChoose_batchEdit_field_value(self, fieldValue):
        self.click('choose_batchEdit_field_value')
        locator = self.parser_element('batchEdit_field_value_list')['path']
        WebDriverWait(self.driver, 30).until(lambda the_driver:the_driver.find_element(by=By.XPATH, value=locator).is_enabled())
        item_list = self.driver.find_element(by=By.XPATH, value=locator).find_elements(by=By.TAG_NAME, value='li')
        for item in item_list:
            if str(item.text).__contains__(fieldValue):
                item.click()
                break
            else:
                continue

    # 批量编辑后点击保存
    def click_batchEdit_save_button(self):
        self.click('click_batchEdit_save_button')

    # 通用单据详情页新建，参数为详情页关联新建时的文案
    def new_data_on_detail(self, form_name):
        # 详情页触发新建入口的下拉选项
        trigger_locator = self.parser_element('trigger_new_data_on_detail')['path']
        new_item_locator = self.parser_element('new_item_container_on_detail')['path']
        # 触发详情页新建
        WebDriverWait(self.driver, 30).until(lambda the_driver:the_driver.find_element(by=By.XPATH,value=trigger_locator).is_displayed())
        self.driver.find_element(by=By.XPATH, value=trigger_locator).click()
        time.sleep(2)
        form_new_list = self.driver.find_element(by=By.XPATH, value=new_item_locator).find_elements(by=By.TAG_NAME,value='li')
        for form in form_new_list:
            if str(form.text).__contains__(form_name):
                form.click()
                break
            else:
                continue

    # 详情页创建默认的下游业务单据
    def add_default_link_form_on_detail(self, count):
        if count == 'multiple':
            # 详情页多个操作选择项时，点击默认按钮创建下游数据
            self.click('add_default_form_on_detail_multiple_options')
        else:
            # 详情页只有一个操作项，点击默认按钮创建下游数据
            self.click('add_default_form_on_detail_single')

    # 通用 详情页切换tab栏, 不支持(更多)
    def switch_tab_on_detail(self, tab_name):
        tab_list_container = self.parser_element('tab_list_on_detail')['path']
        tab_more = self.parser_element('tab_more_on_detail')['path']
        # 详情页tab栏
        WebDriverWait(self.driver, 30).until(lambda the_driver:the_driver.find_element(by=By.XPATH, value=tab_list_container).is_displayed())
        tab_list = self.driver.find_element(by=By.XPATH, value=tab_list_container).find_elements(by=By.TAG_NAME,value='span')
        for tab in tab_list:
            if str(tab.text).__contains__(tab_name):
                tab.click()
                time.sleep(4)
                break
            else:
                continue

    # 通过详情页tab穿透到数据详情
    def go_to_data_detail_by_detailtab(self, link_data_index):
        WebDriverWait(self.driver, 30).until(lambda the_driver:the_driver.find_elements(by=By.XPATH, value='//div[@class="id"]/a'))
        data_list = self.driver.find_elements(by=By.XPATH, value='//div[@class="id"]/a')
        for data in data_list:
            if data_list.index(data) == link_data_index:
                data.click()
        time.sleep(4)

    # (通用)详情页团队成员信息
    def get_team_data(self):
        team_list = []
        team_data = self.find_elements("team_data_on_detail")
        for team in team_data:
            team_info = team.find_element(by=By.TAG_NAME, value="span").text
            team_list.append(team_info)
        return team_list

    # 通用 详情页操作(不支持更多)
    def operate_on_detail(self, operate_name):
        # 详情页tab栏
        locator = self.parser_element('operate_list_on_detail')['path']
        WebDriverWait(self.driver, 30).until(lambda the_driver: the_driver.find_element(by=By.CLASS_NAME, value=locator).is_displayed())
        operate_list = self.driver.find_element(by=By.CLASS_NAME, value=locator).find_elements(by=By.TAG_NAME,value='span')
        for operate in operate_list:
            if str(operate.text).__contains__(operate_name):
                # operate.click()
                ActionChains(self.driver).move_to_element(operate).click().perform()
                break
            else:
                continue
        time.sleep(4)

    # 详情页更多操作
    def operate_more_on_detail(self, operation_name):
        # 点击详情页更多
        WebDriverWait(self.driver, 30).until(lambda the_driver: the_driver.find_element(by=By.CLASS_NAME, value="el-icon-more").is_enabled())
        action_element = self.driver.find_element(by=By.CLASS_NAME, value="el-icon-more")
        # if operation_name == "删除":
        #     action_element = self.find_element("delete_in_more_on_detail")
        #     ActionChains(self.driver).move_to_element(action_element).click().perform()
        # elif operation_name == "打印":
        #     action_element = self.find_element("print_in_more_on_detail")
        #     ActionChains(self.driver).move_to_element(action_element).click().perform()
        # else:
        #     pass
        ActionChains(self.driver).move_to_element(action_element).perform()
        time.sleep(1)
        locator = self.parser_element('button_in_more_on_detail')['path']
        button_name_list = self.driver.find_elements(by=By.XPATH, value=locator)
        for button in button_name_list:
            if str(button.text).__contains__(operation_name):
                button.click()
                break
            else:
                continue
        time.sleep(3)

    # 跟进记录详情页的操作
    def operate_on_follow_record(self, operate_index):
        locator = self.parser_element('follow_record_operate')['path']
        WebDriverWait(self.driver, 30).until(lambda the_driver: the_driver.find_element(by=By.XPATH, value=locator).is_displayed())
        operate_list = self.driver.find_elements(by=By.XPATH, value=locator)
        for operate in operate_list:
            if operate_list.index(operate) == operate_index:
                ActionChains(self.driver).move_to_element(operate).click().perform()
                break
            else:
                continue

    # 通用：切换多模板
    def switch_template_on_list(self, template_name):
        # 多模板tab栏
        locator = self.parser_element('template_on_list')['path']
        WebDriverWait(self.driver, 30).until(lambda the_driver: the_driver.find_elements(by=By.XPATH, value=locator).is_displayed())
        template_list = self.driver.find_element(by=By.XPATH, value=locator).find_elements(by=By.TAG_NAME,value='span')
        for template in template_list:
            if str(template.text).__contains__(template_name):
                template.click()
                break
            else:
                continue

    # 关闭当前数据的详情页
    def close_current_data_detail(self):
        self.click('close_current_data_detail')
        time.sleep(1)

    # 接收参数为关联数据的索引
    def select_link_data(self, link_data_index):
        link_data_trigger_list = self.find_elements('link_data')
        link_data_trigger_list[link_data_index].click()
        self.click("the_first_link_data")
        self.click('sure_link_data_select')


    # 选择产品，只支持全选
    def select_products(self):
        time.sleep(4)
        # 点击选择产品
        self.click("trigger_select_product")
        # 在产品选择浮层中点击全选
        self.click("select_all_product")
        # 点击确定
        self.click("sure_selected_product")

    # 子表单内选择产品
    def select_product_in_subform(self, select_index):
        # 点击选择产品
        link_data_trigger_list = self.find_elements('trigger_select_product_in_subform')
        link_data_trigger_list[select_index].click()
        # 在产品选择浮层中选择第一个产品
        self.click("select_first_product")
        # 点击确定
        self.click("sure_selected_product")

    # 根据弹窗里面的产品名称选择产品

    # 填写新建表单页（基础信息）的第一个文本框内容
    def set_first_text_input_box_in_basics(self, text_value):
        self.input("first_text_input_box_in_basics", text_value)

    # 填写新建表单页(扩展信息、退货信息、采购信息等属于表单的第二个模块)的第一个文本框内容
    def set_first_text_input_box_about_others(self, text_value):
        self.input("first_text_input_box_about_others", text_value)

    # 填写个人客户姓名
    def set_customer_name(self, text_value):
        self.input("customer_name_input", text_value)

    # 保存单据(新建/编辑页保存)
    def save_form(self, position):
        #position为提交按钮的位置，因为草稿箱功能导致保存按钮位置有所不一样  2为修改页面或者快捷新建页面没有草稿箱功能的位置  3是有草稿箱功能时，保存按钮的位置
        if position == 2:
            self.click("save_form_data2")
        else:
            self.click("save_form_data3")

    # 保存单据并确认对话框
    def save_and_sure_dialog(self):
        self.click("save_form_data")
        # 扯淡的逻辑，是不是会提示有过期产品
        if self.find_element("sure_on_message_dialog") is None:
            pass
        else:
            self.click("sure_on_message_dialog")

    # 对话框中点击确认
    def sure_on_message_dialog(self):
        self.click("sure_on_message_dialog")

    # 列表页搜索
    def search_on_list(self, search_condition):
        self.input("search_condition_on_list", search_condition)
        time.sleep(1)
        self.click("page_title")
        time.sleep(4)

    # 列表页切换分组
    def switch_group_on_list(self, group_name):
        group_list = self.find_elements("group_on_list")
        for group in group_list:
            if group_name in str(group.text):
                ActionChains(self.driver).move_to_element(group).click().perform()
                time.sleep(1)
                break
            else:
                continue

    # 清空列表页搜索条件
    def clear_search_condition_on_list(self):
        self.click("clear_search_condition")
        time.sleep(4)

    # 根据页面文字从一组元素中选中一个
    def choice_by_characters(self, element_key, choose_character):
        element_list = self.find_elements(element_key)
        for element in element_list:
            element_text = element.text
            if str(choose_character) in str(element_text):
                element.click()
            else:
                pass

    # 根据索引从一组元素中选中一个
    def choice_by_index(self, element_key, choose_index):
        element_list = self.find_elements(element_key)
        element_list[choose_index].click()

    # 获取一组元素的文本值
    def get_elements_text(self, parent_element_key):
        element_text_list = []
        element_data = self.parser_element(parent_element_key)
        element_tag = element_data['element_tag']
        parent_element = self.find_element(parent_element_key)
        element_list = parent_element.find_elements(by=By.CLASS_NAME, value=element_tag)
        for element in element_list:
            element_text = element.text
            element_text_list.append(element_text)
        return element_text_list

    # 编辑表单数据成员---->在常用成员列表选择
    def switch_owner(self, memberName):
        # 切换到公司全部成员选择
        self.driver.find_element(by=By.XPATH, value="//div[@class='el-scrollbar__view']/div/div[2]/div[1]/span[2]").click()
        # 获取成员列表
        locator = self.parser_element("common_member_list")['path']
        WebDriverWait(self.driver, 30).until(lambda the_driver:the_driver.find_element(by= By.XPATH, value=locator))
        member_list = self.driver.find_elements(by= By.XPATH, value=locator)
        for member in member_list:
            if str(member.text).__contains__(memberName):
                member.click()
                self.click('member_list_sure')
                break
            else:
                continue
        time.sleep(4)

    # 断言-元素文本值包含预期的文本
    def assert_single_element_text_contains(self, element_key, expected_text):
        actual_text = self.get_element_text(element_key)
        with soft_assertions():
            assert_that(actual_text).contains(expected_text)

    # 判断列表页有数据
    def assert_list_have_data(self, data_text):
        WebDriverWait(self.driver, 30).until(lambda the_driver: the_driver.find_element(by=By.PARTIAL_LINK_TEXT, value=data_text))
        data_on_list = self.driver.find_element(by=By.PARTIAL_LINK_TEXT, value=data_text)
        with soft_assertions():
            # # 判断列表页为空的元素不存在
            assert_that(data_on_list).is_not_none()

    # 断言列表不存在此数据
    def assert_list_no_have_data(self, data_text):
        time.sleep(4)
        data_on_list = self.driver.find_elements(by=By.PARTIAL_LINK_TEXT, value=data_text)
        with soft_assertions():
            # 判断列表页为空的元素不存在
            assert_that(data_on_list).is_equal_to([])

    # 判断详情页基本信息字段值，注意断言的值是数组形式
    def assert_detail(self, expected_text_key):
        element_text_list = []
        # 搜集详情页字段的值
        for field_class in ['field-see__left', 'input-text-area-pre', 'number-value__block', 'input-text-see', 'field-label--tap'
                            'input-view-address', 'formdata-form-dept-radio','field-item']:
            # 单行文本（field-see__left）
            if field_class in ('field-see__left'):
                element_list = self.driver.find_elements(by=By.CLASS_NAME, value=field_class)
                for element in element_list:
                    element_text = element.get_dom_attribute("title")
                    element_text_list.append(element_text)
            # 多行文本（input-text-area-pre）、数字/日期（number-value__block）
            elif field_class in ('input-text-area-pre', 'number-value__block'):
                element_list = self.driver.find_elements(by=By.CLASS_NAME, value=field_class)
                for element in element_list:
                    element_text = element.text
                    # 判断div的text是不是空的，如果是空的代表是下拉框或阶段推进器类型的：下拉框（number-value__block）；阶段推进器（number-value__block）
                    if element_text == "":
                        element_text = element.find_element(by=By.TAG_NAME, value="span").text
                        element_text_list.append(element_text)
                    else:
                        # 如果不是空的 那就是数字日期类型，就直接获取文本
                        element_text_list.append(element_text)
            # 单选按钮、复现按钮、下拉复选(input-text-see);关联数据主键(field-label--tap)；
            elif field_class in ('input-text-see', 'field-label--tap'):
                element_list = self.driver.find_elements(by=By.CLASS_NAME, value=field_class)
                for element in element_list:
                    element_text = element.find_element(by=By.TAG_NAME, value="span").text
                    element_text_list.append(element_text)
            # 地址（input-view-address）；部门单选（formdata-form-dept-radio）；
            elif field_class in ('input-view-address', 'formdata-form-dept-radio'):
                element_list = self.driver.find_elements(by=By.CLASS_NAME, value=field_class)
                for element in element_list:
                    element_text = element.find_element(by=By.TAG_NAME, value="div").text
                    element_text_list.append(element_text)
            # 成员单选/创建人；
            elif field_class in ('field-item'):
                # 获取成员单选/创建人
                creator_element_list = self.driver.find_elements(by=By.XPATH, value="//div[@class='field-item']/div/span")
                owner_element_list = self.driver.find_elements(by=By.XPATH, value="//div[@class='field-item']/div")
                for element in creator_element_list,owner_element_list:
                    element_text = element.find_element(by=By.TAG_NAME, value="span").text
                    element_text_list.append(element_text)
            else:
                pass
        # 遍历期望值，逐个判断
        expect_field_text_list = self.parser_element(expected_text_key)
        for expected_text in expect_field_text_list:
            with soft_assertions():
                assert_that(str(element_text_list)).contains(expected_text)

    # 判断详情页数据的标签，标题，摘要
    def assert_title(self, expected_text_key, mark):
        data_title_field_text_list = []
        # 如果mark等于1 则详情页的元素定位为第一种（供应商+供应商联系人+采购合同+采购入库单+采购退货单）：不同业务的元素定位不通用引起的
        if mark == 1:
            # 数据标题
            data_title = self.driver.find_element(by=By.XPATH, value='//*[@class="title-wrap"]/div[2]/div[1]/div[1]/div[1]').text
            data_title_field_text_list.append(data_title)
            # 数据标签
            data_tag_elements = self.driver.find_element(by=By.XPATH, value="//div[@class='title-wrap']/div[2]/div[2]/div").find_elements(by=By.TAG_NAME, value="span")
            if data_tag_elements == []:
                pass
            else:
                for data_tag in data_tag_elements:
                    tag_text = data_tag.text
                    data_title_field_text_list.append(tag_text)
        # 如果mark等于2 则详情页的元素定位为第一种（采购退货出库单, 生产业务详情）
        elif mark == 2:
            #数据标题
            data_title1 = self.driver.find_element(by=By.XPATH, value='//*[@class="title-wrap"]/div/div[1]/div[1]/div[1]').text
            data_title_field_text_list.append(data_title1)
            # 数据标签
            data_tag_elements = self.driver.find_element(by=By.XPATH, value="//div[@class='title-wrap']/div/div[2]/div").find_elements(by=By.TAG_NAME, value="span")
            if data_tag_elements == []:
                pass
            else:
                for data_tag in data_tag_elements:
                    tag_text = data_tag.text
                    data_title_field_text_list.append(tag_text)
        else:
            pass
        # 数据摘要
        data_sub_title_elements = self.driver.find_elements(by=By.XPATH, value='//*[@class="sub-title el-row"]/div/div/p')
        for sub_title in data_sub_title_elements:
            data_title_field_text_list.append(sub_title.text)
        # 遍历期望值，逐个判断
        expect_field_text_list = self.parser_element(expected_text_key)
        for expected_text in expect_field_text_list:
            with soft_assertions():
                assert_that(str(data_title_field_text_list)).contains(expected_text)

    # 判断列表页展示的文本
    def assert_list(self, expected_text_key):
        element_text_list = []
        form_list = self.driver.find_element(by=By.CLASS_NAME, value="el-table__row").find_elements(by=By.TAG_NAME, value="td")
        # 收集列表页的文本信息
        for cell_td in form_list:
            cell_div_text = cell_td.find_element(by=By.TAG_NAME, value="div").text
            if cell_div_text is None:
                cell_span_text = cell_td.find_element(by=By.TAG_NAME, value="div").find_element(by=By.TAG_NAME, value="span")
                element_text_list.append(cell_span_text)
            else:
                element_text_list.append(cell_div_text)
        # 对列表的预期字段进行断言
        expected_text_list_on_list = self.parser_element(expected_text_key)
        for expected_text in expected_text_list_on_list:
            with soft_assertions():
                assert_that(str(element_text_list)).contains(expected_text)

    # 判断打印预览页的文本信息, 打印预览页的预期必须是数组形式
    def assert_print_data(self, print_data_key):
        # 打印预览页 展示的文案
        element_text_list_on_print = []
        data_list_on_print = self.parser_element(print_data_key)
        WebDriverWait(self.driver, 30).until(lambda the_driver: the_driver.find_element(by=By.ID, value="pdfDom").is_displayed())
        print_detail_fields = self.driver.find_element(by=By.ID, value="pdfDom").find_elements(by=By.CLASS_NAME, value="word-break")
        for field in print_detail_fields:
            element_text_list_on_print.append(field.text)
        # 打印预览页的关联产品信息
        print_table_td_list = self.driver.find_element(by=By.XPATH, value="//table[@class='original-table']/tbody/tr[1]").find_elements(by=By.TAG_NAME, value="td")

        # 列表页展示的文案
        for td_container in print_table_td_list:
            field_text = td_container.find_element(by=By.TAG_NAME, value="div").find_element(by=By.TAG_NAME, value="span").text
            element_text_list_on_print.append(field_text)
        # 对打印预览页的预期字段进行断言
        for expected_text in data_list_on_print:
            with soft_assertions():
                assert_that(str(element_text_list_on_print)).contains(expected_text)

    # 对关联产品进行断言, 关联产品的预期信息必须是数组形式
    def assert_relate_production(self, product_tab_data_list):
        # (系统)关联产品tab栏
        product_text_list = []
        product_tab_td_list = self.driver.find_element(by=By.XPATH, value="//div[@class='table-form__content']/div/div/div[3]/table/tbody/tr[1]"). \
            find_elements(by=By.TAG_NAME, value="td")

        # 关联产品展示的文案
        for td_index in range(1, len(product_tab_td_list)):
            field_text = product_tab_td_list[td_index].find_element(by=By.TAG_NAME, value="div").find_element(by=By.TAG_NAME, value="span").text
            product_text_list.append(field_text)

        # 对关联产品的预期字段进行断言
        for expected_text in product_tab_data_list:
            with soft_assertions():
                assert_that(str(product_text_list)).contains(expected_text)

    # 对负责团队进行断言, 团队成员必须是数组形式
    def assert_team_data(self, team_name_list):
        team_text_list = []
        team_list = self.find_elements("team_data_on_detail")
        for team in team_list:
            team_text_list.append(team.text)
        # 判断团队成员包含
        for expected_text in team_name_list:
            with soft_assertions():
                assert_that(str(team_text_list)).contains(expected_text)

    # 对弹窗内容进行断言
    def assert_alert_text(self, expected_text):
        locator = self.parser_element('alert')['path']
        WebDriverWait(self.driver, 30).until(lambda the_driver:the_driver.find_element(by=By.XPATH, value=locator).is_displayed())
        actual_text = self.driver.find_element(by=By.XPATH, value=locator).text
        with soft_assertions():
            assert_that(actual_text.__contains__(expected_text))

    # 输入子表单电话值
    def input_subform_phone(self, phone_value):
        self.input('subform_phone', phone_value)

    # 特殊方法 对供应商跟进记录数量进行断言
    def assert_supplier_communicate_list_quantity_summary(self, expected_text):
        locator = self.parser_element('supplier_communicate_list_quantity_summary')['path']
        WebDriverWait(self.driver, 30).until(
            lambda the_driver: the_driver.find_element(by=By.XPATH, value=locator).is_displayed())
        actual_text = self.driver.find_element(by=By.XPATH, value=locator).text
        with soft_assertions():
            assert_that(actual_text.__contains__(expected_text))

